#pragma once
#include "蛇和食物初始化.h"
#include<stdio.h>
#include<windows.h>
#include<time.h>
#include"游戏主题函数.h"
int max_score;
int score;
int speed_v;
//将光标设置到坐标x y处
void set(int x, int y)
{
	COORD pos = { x,y };
	SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
}

//此函数是判断生成的食物是否与蛇重合,若是则返回1，否则返回0
int isonSnake(COORD food)
{
	int i;
	for (i = 0; i < snake.size; i++)
	{
		if (food.X == snake.body[i].X && food.Y == snake.body[i].Y)
			return 1;
	}
	return 0;
}
void initUI()
{
	//srand(time(NULL));
	system("cls");
	FILE* fp = fopen("最高分.txt", "r");
	fscanf(fp, "%d", &max_score);
	int i = 0;
	int j = 0;
	COORD coord;
	COORD coord_food;

	coord_food.X = food.X;
	coord_food.Y = food.Y;
	//设置窗口大小69*69
	SMALL_RECT rect = { 0, 0, WIDE+20, HIGH+5 };
	SetConsoleWindowInfo(GetStdHandle(STD_OUTPUT_HANDLE), TRUE, &rect);

	COORD size = { WIDE+20,HIGH+5 };
	SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE), size);
	//画边框
	set(0, 0);
	printf("┏");
	for (i = 1; i < WIDE; i++)
	{
		set(i, 0);
		printf("━");
		set(i, HIGH);
		printf("━");
	}
	set(WIDE, 0);
	printf("┓");
	for (i = 1; i < HIGH; i++)
	{
		set(0, i);
		printf("┃");
		set(WIDE, i);
		printf("┃");
	}
	set(0, HIGH);
	printf("┗");
	set(WIDE, HIGH);
	printf("┛");
	set(WIDE + 1, 1);
	printf("目前得分为:%d", score);
	set(WIDE + 1, 2);
	printf("历史最高为:%d", max_score);
	set(WIDE + 1, 3);
	printf("速度为:%d", speed_v);
	//判断随机生成的食物位置是否与蛇的位置或地图边界重合，若重合就重新生成一个
	
	do
	{
		initFood();
		coord_food.X = food.X;
		coord_food.Y = food.Y;
	} while (
		coord_food.X == 0 
		|| coord_food.Y == 0 
		|| coord_food.X == WIDE 
		|| coord_food.Y == HIGH
		||isonSnake(coord_food));

	//画蛇和食物
	for (i = 0; i < snake.size; i++)
	{
		coord.X = snake.body[0 + i].X;
		coord.Y = snake.body[0 + i].Y;
		SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
		printf("*");
	}
	SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord_food);
	printf("#");

}